<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Dynamic_routers
 *
 * @author lucas
 */
class Dynamic_routers extends ADM_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('permissions');

        $this->load->add_module('settings');
        $this->load->config('settings/parametros');
        $this->load->model('settings/settings_model');
    }

    function index() {
        $this->load->helper('file');
        $b_consultar = $this->permissions->get_acoes_menu();

        if (fileperms(APPPATH . 'config/routes.php') != "33279") {

            echo "Arquivo sem permissao de Escrita. <br> /public_html/application/config/routes.php dever ter permissao chmod 777";
        } else {


            $rotas = "";
            $rotas .= "<?php  if (!defined('BASEPATH'))
    exit('No direct script access allowed'); " . "\n\n\n\n";

            include (APPPATH . 'modules/' . $this->settings_model->get_valor("template_site") . '/config/routes_manual.php');

            if (isset($route)) {
                foreach ($route as $key => $value) {
                    $rotas .= '$route["' . $key . '"] = "' . $value . '";' . "\n";
                }
            }

            //$rotas .= '$route["default_controller"] = "login/cms/login_controller/index"; ' . "\n";
            //$rotas .= '$route["404_override"] = "erros_controller/error_404";' . "\n";

            
            $rotas .= "\n\n";

            if ($b_consultar != null) {
                foreach ($b_consultar as $k => $v) {
                    $any = '';
                    $anys = '';
                    $rotas .= "//Ações do Controller e Funcion com paramentro " . $v->function;
                    $rotas .= "\n";

                    $explode_url = explode('/', $v->url);
                    $explode_url_1 = $explode_url[0];
                    unset($explode_url[0]);

                    $_url = implode('/', $explode_url);

                    for ($i = 1; $i <= 10; $i++) {
                        $rotas .= '$route["cms/' . $v->url . $any . '"] = "' . $explode_url_1 . '/cms/' . $_url . $anys . '";' . "\n";
                        $rotas .= '$route["cms/' . $v->url . $any . '"] = "' . $explode_url_1 . '/cms/' . $_url . $anys . '";' . "\n";
                        $any .= '/(:any)';
                        $anys .= '/$' . $i;
                    }
                    $rotas .= "\n\n";
                }
            }

            $rotas .= '$route["cms"] = "login/cms/login/index"; ' . "\n";
            $rotas .= '$route["cms/rota"] = "user/cms/dynamic_routers"; ' . "\n";
            $rotas .= "\n\n\n";
            $rotas .= '/* End of file routes.php */' . "\n";
            $rotas .= '/* Location: ./application/config/routes.php */' . "\n";
            write_file(APPPATH . 'config/routes.php', $rotas);
            $this->_show_message("As rotas dinamicas geradas com sucesso.", "Rotas");

            //var_dump($rotas);
            unset($rotas);
        }
    }

}